package com.xuesi.demoadmin.service.impl;

import com.xuesi.demoadmin.entity.R;
import com.xuesi.demoadmin.entity.User;
import com.xuesi.demoadmin.service.IAuthService;
import com.xuesi.demoadmin.utils.Md5Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements IAuthService {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private TokenServiceImpl tokenService;

    @Override
    public R<User> login(String phone, String password) {
        // 查询用户是否存在
        User user = this.userService.selectUserByPhone(phone);

        if (user == null) {
            return new R<User>(400, "用户不存在");
        }

        String encodePassword = Md5Utils.hash(password);
        if (!user.getPassword().equals(encodePassword)) {
            return new R<User>(400, "用户名或密码错误");
        }

        String token = this.tokenService.encode(user.getUserId());
        user.setToken(token);

        return new R<User>(200, "success", user);
    }

    @Override
    public void logout(String token) {
        // TODO Auto-generated method stub
    }
}
